/*
 * 文件名：ZillaURL.java
 * 版权：(C)版权所有2013-2013 com.zilla
 * 描述：Zilla模块跳转协议
 * 修改人：ze.chen
 * 修改时间：2013-6-6 下午1:39:08
 */
package com.zilla.android.zillacore.libzilla.module;


import com.github.snowdream.android.util.Log;
import com.zilla.android.zillacore.libzilla.module.exception.MalformedZillaURLException;

/**
 * zilla模块跳转协议<br>
 * Zilla模块跳转协议，为了能实现模块之间的松耦合，模块之间的跳转将使用该协议实现<br>
 * 形如：协议/模块/模块下级页面/参数
 *
 * @author ze.chen
 * @version 1.0
 * @date 2013-6-6 下午1:39:08
 */

public final class ZillaURL {
    /**
     * zilla
     */
    private String protocol;
    /**
     * zilla://module
     */
    private String module;
    /**
     * loging/logindetail
     */
    private String path;
    /**
     * name=xxx&psw=xxx
     */
    private String query;

    private static final String ZILLA_PROTOCAL = "zilla";

    public ZillaURL(String spec) throws MalformedZillaURLException {
        if ("".equals(spec) || !spec.startsWith(ZILLA_PROTOCAL + "://")) {
            throw new MalformedZillaURLException("protocal is not zilla://");
        }
        String protocalNext = spec.substring((ZILLA_PROTOCAL + "://").length());
        // 如果url中含有/或者?，则module为?或/之前的内容
        int module_end = protocalNext.length();
        if (protocalNext.contains("?")) {
            int queylength = protocalNext.indexOf("?");
            module_end = (queylength < module_end ? queylength : module_end);
            // query
            query = protocalNext.substring(queylength + 1);
            Log.i("query-->" + query);
        }
        if (protocalNext.contains("/")) {
            int pathlength = protocalNext.indexOf("/");
            module_end = (pathlength < module_end ? pathlength : module_end);
            // path
            String pathNext = protocalNext.substring(module_end);
            if (pathNext.contains("?")) {
                path = pathNext.substring(0, pathNext.indexOf("?"));
            } else {
                path = pathNext;
            }
            Log.i("path-->" + path);
        }
        module = protocalNext.substring(0, module_end);
        Log.i("module-->" + module);
    }

    public String getModule() {
        return module;
    }

    public void setModule(String module) {
        this.module = module;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }
}
